package cn.ym.basic.service.impl;

import cn.ym.basic.mapper.BaseMapper;
import cn.ym.basic.query.BaseQuery;
import cn.ym.basic.service.BaseService;
import cn.ym.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired//注入对象
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {

        return baseMapper.findAll();
    }

    @Override
    public T findOne(Long id) {

        return baseMapper.findOne(id);
    }

    @Transactional
    @Override
    public void delById(Long id) {

        baseMapper.delById(id);
    }

    @Transactional
    @Override
    public void add(T t) {

        baseMapper.add(t);
    }

    @Transactional
    @Override
    public void updateById(T t) {

        baseMapper.updateById(t);
    }


    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        //1.查询总数量 - 前端计算分页栏
        Integer totals = baseMapper.queryCount(baseQuery);
        if(totals==null){//没有数据
            return new PageList<>();
        }

        //2.查询当前页的分页数据 - 前端展示当前页的数据
        List<T> data = baseMapper.queryData(baseQuery);
        return new PageList<>(totals,data);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        baseMapper.patchDelete(ids);
    }

}
